<template>
  <div class="knowledge-manage-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="header-left">
          <div class="header-icon">
            <Icon icon="ep:reading" />
          </div>
          <div class="header-info">
            <h1 class="page-title">知识管理</h1>
            <p class="page-subtitle">管理和维护客服知识库内容</p>
          </div>
        </div>
        <div class="header-actions">
          <el-button type="primary" @click="openFrom('create')" class="create-btn">
            <Icon icon="ep:plus" class="btn-icon" />
            新增知识
          </el-button>
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="content-wrapper">
      <!-- 统计卡片 -->
      <div class="stats-section">
        <div class="stats-grid">
          <div class="stat-card">
            <div class="stat-icon general-icon">
              <Icon icon="ep:document" />
            </div>
            <div class="stat-content">
              <div class="stat-number">{{ generalCount }}</div>
              <div class="stat-label">一般业务</div>
            </div>
          </div>

          <div class="stat-card">
            <div class="stat-icon problem-icon">
              <Icon icon="ep:warning" />
            </div>
            <div class="stat-content">
              <div class="stat-number">{{ problemCount }}</div>
              <div class="stat-label">业务问题</div>
            </div>
          </div>

          <div class="stat-card">
            <div class="stat-icon total-icon">
              <Icon icon="ep:collection" />
            </div>
            <div class="stat-content">
              <div class="stat-number">{{ total }}</div>
              <div class="stat-label">总计</div>
            </div>
          </div>
        </div>
      </div>

      <!-- 搜索和筛选 -->
      <div class="filter-section">
        <div class="filter-content">
          <div class="search-box">
            <el-input
              v-model="searchKeyword"
              placeholder="搜索标题或关键词..."
              @input="handleSearch"
              class="search-input"
            >
              <template #prefix>
                <Icon icon="ep:search" />
              </template>
            </el-input>
          </div>

          <div class="filter-controls">
            <el-select
              v-model="filterType"
              placeholder="问题类型"
              @change="handleFilter"
              class="type-filter"
            >
              <el-option label="全部类型" value="" />
              <el-option label="一般业务" :value="1" />
              <el-option label="业务问题" :value="2" />
            </el-select>

            <el-button @click="resetFilters" class="reset-btn">
              <Icon icon="ep:refresh" class="btn-icon" />
              重置
            </el-button>
          </div>
        </div>
      </div>

      <!-- 知识列表 -->
      <div class="table-section">
        <div class="table-header">
          <div class="table-title">
            <Icon icon="ep:list" class="title-icon" />
            <span>知识列表</span>
          </div>
          <div class="table-info">
            <span class="info-text">共 {{ total }} 条记录</span>
          </div>
        </div>

        <div class="table-container">
          <el-table
            :data="list"
            v-loading="loading"
            class="knowledge-table"
            empty-text="暂无知识数据"
          >
            <el-table-column prop="title" label="标题" min-width="200">
              <template #default="scope">
                <div class="title-cell">
                  <div class="title-icon-wrapper">
                    <Icon icon="ep:document" class="title-doc-icon" />
                  </div>
                  <div class="title-content">
                    <span class="title-text">{{ scope.row.title }}</span>
                  </div>
                </div>
              </template>
            </el-table-column>

            <el-table-column prop="keys" label="关键词" min-width="180" show-overflow-tooltip>
              <template #default="scope">
                <div class="keywords-cell">
                  <el-tag
                    v-for="(keyword, index) in getKeywordArray(scope.row.keys)"
                    :key="index"
                    size="small"
                    class="keyword-tag"
                  >
                    {{ keyword }}
                  </el-tag>
                </div>
              </template>
            </el-table-column>

            <el-table-column prop="knowledgeType" label="问题类型" width="120" align="center">
              <template #default="scope">
                <el-tag
                  :type="scope.row.knowledgeType == 1 ? 'primary' : 'warning'"
                  class="type-tag"
                >
                  <Icon
                    :icon="scope.row.knowledgeType == 1 ? 'ep:document' : 'ep:warning'"
                    class="tag-icon"
                  />
                  {{ scope.row.knowledgeType == 1 ? '一般业务' : '业务问题' }}
                </el-tag>
              </template>
            </el-table-column>

            <el-table-column label="操作" width="160" align="center" fixed="right">
              <template #default="scope">
                <div class="action-buttons">
                  <el-button
                    type="primary"
                    text
                    @click="openFrom('update', scope.row.id)"
                    class="action-btn edit-btn"
                  >
                    <Icon icon="ep:edit" class="btn-icon" />
                    编辑
                  </el-button>
                  <el-button
                    type="danger"
                    text
                    @click="del(scope.row.id)"
                    class="action-btn delete-btn"
                  >
                    <Icon icon="ep:delete" class="btn-icon" />
                    删除
                  </el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>

        <!-- 分页 -->
        <div class="pagination-wrapper">
          <Pagination
            :total="total"
            v-model:page="queryParams.pageNo"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
            class="custom-pagination"
          />
        </div>
      </div>
    </div>
  </div>

  <!-- 表单弹窗 -->
  <SuperForm ref="superRef" @success="getList" />
</template>

<script setup>
import * as defaultIvrKnowApi from '@/api/call/manage'
import SuperForm from './superFrom.vue'

defineOptions({ name: 'KnowledgeManage' })

// 响应式数据
const loading = ref(false)
const searchKeyword = ref('')
const filterType = ref('')
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  flowId: 0,
  title: '',
  knowledgeType: ''
})

const superRef = ref()
const list = ref([])
const total = ref(0)

// 统计数据
const generalCount = computed(() => {
  return list.value.filter((item) => item.knowledgeType == 1).length
})

const problemCount = computed(() => {
  return list.value.filter((item) => item.knowledgeType == 2).length
})

// 获取列表数据
const getList = async () => {
  loading.value = true
  try {
    const data = await defaultIvrKnowApi.getIvrknow(queryParams)
    list.value = data.list
    total.value = data.total
  } catch (error) {
    console.error('获取知识列表失败:', error)
  } finally {
    loading.value = false
  }
}

// 打开表单
const openFrom = (type, id) => {
  superRef.value.open(type, id)
}

// 删除操作
const del = async (id) => {
  try {
    await ElMessageBox.confirm('确定删除此条知识吗？', '删除确认', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    await defaultIvrKnowApi.deleteIvrknow(id)
    ElMessage.success('删除成功')
    await getList()
  } catch (error) {
    if (error !== 'cancel') {
      console.error('删除失败:', error)
    }
  }
}

// 搜索处理
const handleSearch = () => {
  queryParams.title = searchKeyword.value
  queryParams.pageNo = 1
  getList()
}

// 筛选处理
const handleFilter = () => {
  queryParams.knowledgeType = filterType.value
  queryParams.pageNo = 1
  getList()
}

// 重置筛选
const resetFilters = () => {
  searchKeyword.value = ''
  filterType.value = ''
  queryParams.title = ''
  queryParams.knowledgeType = ''
  queryParams.pageNo = 1
  getList()
}

// 处理关键词数组
const getKeywordArray = (keywords) => {
  if (!keywords) return []
  return keywords
    .split(',')
    .filter((k) => k.trim())
    .slice(0, 3)
}

// 初始化
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.knowledge-manage-container {
  padding: 20px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;
}

/* 页面头部 */
.page-header {
  background: linear-gradient(135deg, #97a5e2 0%, #7db5db 100%);
  border-radius: 16px;
  padding: 12px;
  margin-bottom: 12px;
  color: white;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    width: 150px;
    height: 150px;
    background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 0%, transparent 70%);
    border-radius: 50%;
  }
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  z-index: 1;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.header-icon {
  width: 48px;
  height: 48px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  backdrop-filter: blur(10px);
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  margin: 0 0 4px 0;
}

.page-subtitle {
  font-size: 14px;
  margin: 0;
  opacity: 0.9;
}

.create-btn {
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: white;
  backdrop-filter: blur(10px);
  border-radius: 8px;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.2s ease;

  &:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: translateY(-1px);
  }

  .btn-icon {
    margin-right: 6px;
  }
}

/* 内容区域 */
.content-wrapper {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

/* 统计卡片 */
.stats-section {
  margin-bottom: 0;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
}

.stat-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  padding: 24px;
  display: flex;
  align-items: center;
  gap: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
  }
}

.stat-icon {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 20px;
}

.general-icon {
  background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
}

.problem-icon {
  background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
}

.total-icon {
  background: linear-gradient(135deg, #10b981 0%, #059669 100%);
}

.stat-content {
  flex: 1;
}

.stat-number {
  font-size: 28px;
  font-weight: 700;
  color: #1f2937;
  line-height: 1;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  color: #6b7280;
  font-weight: 500;
}

/* 搜索和筛选 */
.filter-section {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  backdrop-filter: blur(10px);
}

.filter-content {
  display: flex;
  gap: 16px;
  align-items: center;
}

.search-box {
  flex: 1;
  max-width: 400px;
}

.search-input {
  :deep(.el-input__wrapper) {
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  }
}

.filter-controls {
  display: flex;
  gap: 12px;
  align-items: center;
}

.type-filter {
  width: 140px;

  :deep(.el-select__wrapper) {
    border-radius: 8px;
  }
}

.reset-btn {
  border: 2px solid #e5e7eb;
  color: #6b7280;
  border-radius: 8px;

  &:hover {
    border-color: #0052cc;
    color: #0052cc;
  }

  .btn-icon {
    margin-right: 4px;
  }
}

/* 表格区域 */
.table-section {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  backdrop-filter: blur(10px);
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid rgba(0, 82, 204, 0.1);
  background: linear-gradient(135deg, rgba(0, 82, 204, 0.02) 0%, transparent 100%);
}

.table-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 18px;
  font-weight: 600;
  color: #1f2937;
}

.title-icon {
  color: #0052cc;
  font-size: 20px;
}

.table-info {
  display: flex;
  align-items: center;
  gap: 16px;
}

.info-text {
  font-size: 14px;
  color: #6b7280;
}

.table-container {
  padding: 0 24px;
}

.knowledge-table {
  :deep(.el-table__header) {
    background: rgba(0, 82, 204, 0.02);
  }

  :deep(.el-table__header th) {
    background: transparent;
    border-bottom: 1px solid rgba(0, 82, 204, 0.1);
    color: #374151;
    font-weight: 600;
  }

  :deep(.el-table__body tr:hover) {
    background: rgba(0, 82, 204, 0.02);
  }

  :deep(.el-table__body td) {
    border-bottom: 1px solid rgba(0, 82, 204, 0.05);
  }
}

.title-cell {
  display: flex;
  align-items: center;
  gap: 12px;
}

.title-icon-wrapper {
  width: 32px;
  height: 32px;
  background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 14px;
  flex-shrink: 0;
}

.title-content {
  flex: 1;
  min-width: 0;
}

.title-text {
  font-size: 14px;
  font-weight: 500;
  color: #374151;
  line-height: 1.4;
}

.keywords-cell {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.keyword-tag {
  background: rgba(0, 82, 204, 0.1);
  color: #0052cc;
  border: 1px solid rgba(0, 82, 204, 0.2);
  border-radius: 12px;
  font-size: 12px;
}

.type-tag {
  display: flex;
  align-items: center;
  gap: 4px;
  border-radius: 12px;
  font-weight: 500;
}

.tag-icon {
  font-size: 12px;
}

.action-buttons {
  display: flex;
  gap: 8px;
  justify-content: center;
}

.action-btn {
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 13px;

  .btn-icon {
    margin-right: 4px;
    font-size: 12px;
  }

  &.edit-btn:hover {
    background: rgba(16, 185, 129, 0.1);
    color: #10b981;
  }

  &.delete-btn:hover {
    background: rgba(239, 68, 68, 0.1);
    color: #ef4444;
  }
}

.pagination-wrapper {
  padding: 20px 24px;
  border-top: 1px solid rgba(0, 82, 204, 0.1);
  background: rgba(0, 82, 204, 0.02);
}

.custom-pagination {
  :deep(.el-pagination) {
    justify-content: center;
  }
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .stats-grid {
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 16px;
  }
}

@media (max-width: 768px) {
  .knowledge-manage-container {
    padding: 12px;
  }

  .page-header {
    padding: 20px;
    margin-bottom: 16px;
  }

  .header-content {
    flex-direction: column;
    gap: 16px;
    align-items: flex-start;
  }

  .stats-grid {
    grid-template-columns: 1fr;
  }

  .filter-content {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }

  .search-box {
    max-width: none;
  }

  .filter-controls {
    justify-content: space-between;
  }

  .table-header {
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
  }

  .table-container {
    padding: 0 12px;
  }

  .pagination-wrapper {
    padding: 16px 12px;
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .knowledge-manage-container {
    background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
  }

  .stat-card,
  .filter-section,
  .table-section {
    background: rgba(30, 41, 59, 0.95);
  }

  .page-title,
  .table-title {
    color: #f9fafb;
  }

  .stat-number {
    color: #f9fafb;
  }

  .stat-label,
  .info-text {
    color: #d1d5db;
  }

  .title-text {
    color: #e5e7eb;
  }
}
</style>
